<?php

namespace App\Sidong\Controllers;

use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class UserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户管理';

    public function index(Content $content)
    {
        return $content
            ->title($this->title())
            ->breadcrumb(['text' => '用户列表'])
            ->body($this->grid());
    }


    public function edit($id, Content $content)
    {
        return $content
            ->title('编辑用户')
            ->breadcrumb(['text' => '用户列表', 'url' => 'users'], ['text' => '编辑用户'])
            ->body($this->form()->edit($id));
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);
        $admin = \Admin::user();
        $grid->model()->where('tenant_id', $admin->tenant_id);
        $grid->disableBatchActions();
        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->actions(function ($actions) {
            // 去掉查看
            $actions->disableView();
            $actions->disableDelete();
        });
        $grid->column('id', 'ID');
        $grid->column('phone', '手机号码');
        $grid->column('avatar_url', '头像')->lightbox(['width' => 35, 'height' => 35]);
        $grid->column('nickname', '昵称');
        $grid->column('realname', '真实姓名');
        $grid->column('weapp_openid', 'OPENID')->copyable();
//        $grid->column('province', '省份');
//        $grid->column('city', '城市');
        $grid->column('gender', '性别')->display(function($gender) {
            return $gender == 1 ? '<span class="label label-success">男</span>' : '<span class="label label-danger">女</span>';
        });
        $grid->column('created_at', '注册时间');
        return $grid;
    }

    protected function form()
    {
        $form = new Form(new User);
        $form->text('phone', '手机号码')->readonly();
        $form->text('weapp_openid', '微信OPENID')->readonly();
        $form->text('nickname', '昵称');
        $form->text('realname', '真实姓名');
        $form->radio('gender', '性别')->options([1 => '男', 2=> '女'])->default(1);
        $form->image('avatar_url', '头像')->crop(128, 128);
        return $form;
    }
}
